import pymysql
# import time
import requests
'直播间蓝光开启情况爬取（功能12）'


# 接口爬虫
def getInterData():
    # 全部直播
    url = "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&tagAll=0&page="
    # 页码
    page = 1
    # 爬取的数据
    result = []
    while True:
        # 获取接口返回数据
        res = requests.get(url + str(page))
        # 转成json格式
        response = res.json()
        # 获取详细内容
        artists = response['data']['datas']
        # 如果返回的为空，就说明爬取完了，退出函数
        if len(artists) == 0:
            break
        result.extend(artists)
        print("第%s页" % page)
        page += 1
        # 防止ip被禁用，限制1秒爬一次
        # time.sleep(1)
    return result


# 获取蓝光数据
def getBlueRay(artists):
    dic = {}
    for i in artists:
        isBlueRay = i["isBluRay"]
        if isBlueRay == "0":
            bluRayMBitRate = '没有开启蓝光'
        else:
            bluRayMBitRate = '蓝光'+i["bluRayMBitRate"]
        # 查看字典里面有没有这个字段（计数方法）
        if bluRayMBitRate not in dic:
            dic[bluRayMBitRate] = 1
        else:
            dic[bluRayMBitRate] = dic[bluRayMBitRate] + 1
    return dic


# 创建MySQL连接函数（当做固定）
def getMysqlConn():
    conn = pymysql.connect(host='106.13.194.7',
                           port=3306,
                           user='rdsroot',
                           password='abc123456',
                           db='huya',
                           charset='utf8')
    # 创建操作的游标
    cursor = conn.cursor()
    # 设置字符编码及自动提交
    cursor.execute('set names utf8')  # 固定格式
    cursor.execute('set autocommit=1')
    # 设置自动提交
    return conn, cursor


# 插入蓝光使用频次
def saveBlueRayData(dic):
    conn, cursor = getMysqlConn()
    # sql语句
    sql = "insert into t_blueRay (id,isBlueRay,blueRayMBitRate,count) values(REPLACE(UUID(),'-',''),%s,%s,%s)"
    for key, value in dic.items():
        if (key == '没有开启蓝光'):
            isBlueRay = 0
        else:
            isBlueRay = 1
        add_data = (isBlueRay, key, value)
        # 这里会把sql语句中的%s替换掉
        cursor.execute(sql, add_data)
    print("mysql插入数据完成")
    # 关闭连接
    cursor.close()
    conn.close()


# 单元测试用
if __name__ == '__main__':
    artists = getInterData()
    blueRayDict = getBlueRay(artists)
    saveBlueRayData(blueRayDict)
